package com.dream.keigezhushou.Activity.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.dream.keigezhushou.Activity.view.OnItemClickListener;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

/**
 * Created by mxmf-001 on 17/3/2.
 */
public class LocalMusicAdapter extends RecyclerView.Adapter<LocalMusicAdapter.MyViewHolder> {
    private Context context;
    private  ArrayList<String>local;


    private View headerView;
    private View footView;

    private static final int HEAD = 1;
    private  static final int FOOT=2;
    public static final int NORMAL = 3;
    private OnItemClickListener itemMoreClickListener;

    public LocalMusicAdapter(Context context, ArrayList<String> local) {
        this.context=context;
        this.local=local;

    }
    public  void setOnItemClickListener(OnItemClickListener itemMoreClickListener){
        this.itemMoreClickListener=itemMoreClickListener;
    }




    public void addHeaderView(View headerView) {
        this.headerView = headerView;
    }
    public int getHeaderViewCount() {
        return headerView == null ? 0 : 1;
    }
    public  void  addFootView(View  footView){
        this.footView=footView;
    }
    public  int getFootViewCount(){
        return footView==null?0:1;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == HEAD) {
            return new MyViewHolder(headerView);
        }
        if(viewType==FOOT){
            return  new MyViewHolder(footView);
        }
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recentplay_rcv, null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        if (getItemViewType(position) == HEAD) return ;
        if(getItemViewType(position)==FOOT) return;

        holder.tvSinger.setText(local.get(position-getHeaderViewCount()));
        holder.iv_more01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (itemMoreClickListener != null) {
                    itemMoreClickListener.onItemMoreClick(position);
                }
            }
        });


//        holder.Linearitem.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//
//                if(myItemClickListener!=null){
//
//                    myItemClickListener.onItemClick(position);
//
//                }
//            }
//        });
        headerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"播放全部",Toast.LENGTH_SHORT).show();


            }
        });
        footView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"删除全部",Toast.LENGTH_SHORT).show();
            }
        });

    }



    @Override
    public int getItemCount() {
        return  local.size() + getHeaderViewCount() + getFootViewCount();
    }
    @Override
    public int getItemViewType(int position) {
        if(position<getHeaderViewCount() )             return  HEAD;

       if(position>=local.size()+getHeaderViewCount()) return  FOOT;
            return  NORMAL;

    }



    public class MyViewHolder extends  RecyclerView.ViewHolder{
        TextView tvMusic;
        TextView tvSinger;
        ImageView iv_more01;

        public LinearLayout Linearitem;
        Button btn_delete;


        public MyViewHolder(View itemView) {

            super(itemView);
            Linearitem=(LinearLayout)itemView.findViewById(R.id.item_music_list_recent);
            iv_more01=(ImageView)itemView.findViewById(R.id.item_more);
            tvSinger=(TextView)itemView.findViewById(R.id.collected_music_singer);
            tvMusic=(TextView)itemView.findViewById(R.id.collected_music_name);
            btn_delete = (Button) itemView.findViewById(R.id.btn_from_delete);
        }
    }

        public void removeItem(int position){
            local.remove(position);
            notifyDataSetChanged();

    }

}
